// pages/myorders/myorders.js
var common = require("../../utils/Conmon.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wuliuList: null,
    wulimodel: true,
    active1: true,
    active2: false,
    active3: false,
    active4: false,
    active5: false,
    active6: false,
    PageIndex: 1,
    PageSize: 3,
    OrderStatus: 'All',
    OrderList: [],
    IsLastPage: false,
    nodata: true,
  },
  wulimodel: function () {
    this.setData({
      wulimodel: false,
    })
  },
  wuliclose: function () {
    this.setData({
      wulimodel: true,
    })
  },
  tabchang1: function () {
    this.setData({
      active1: true,
      active2: false,
      active3: false,
      active4: false,
      active5: false,
      active6: false,
      PageIndex: 1,
      OrderStatus: 'All',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  tabchang2: function () {
    this.setData({
      active1: false,
      active2: true,
      active3: false,
      active4: false,
      active5: false,
      active6: false,
      PageIndex: 1,
      OrderStatus: 'WaitPay',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  tabchang3: function () {
    this.setData({
      active1: false,
      active2: false,
      active3: true,
      active4: false,
      active5: false,
      active6: false,
      PageIndex: 1,
      OrderStatus: 'WaitDeliver',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  tabchang4: function () {
    this.setData({
      active1: false,
      active2: false,
      active3: false,
      active4: true,
      active5: false,
      active6: false,
      PageIndex: 1,
      OrderStatus: 'WaitReceipt',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  tabchang5: function () {
    this.setData({
      active1: false,
      active2: false,
      active3: false,
      active4: false,
      active5: true,
      active6: false,
      PageIndex: 1,
      OrderStatus: 'WaiteValuate',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  tabchang6: function () {
    this.setData({
      active1: false,
      active2: false,
      active3: false,
      active4: false,
      active5: false,
      active6: true,
      PageIndex: 1,
      OrderStatus: 'Refund',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  linkTo: function () {

    wx.switchTab({
      url: '/pages/customIndex/index'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    common.pageInit(function () {
      var Status = that.data.OrderStatus;
      var orderState = wx.getStorageSync('OrderStatusUserInfo');

      if (orderState == '0') {
        that.setData({
          active1: true,
          active2: false,
          active3: false,
          active4: false,
          active5: false,
          active6: false,
          PageIndex: 1,
          OrderStatus: 'All'
        })
        Status = 'All';
      } else if (orderState == '1') {
        that.setData({
          active1: false,
          active2: true,
          active3: false,
          active4: false,
          active5: false,
          active6: false,
          PageIndex: 1,
          OrderStatus: 'WaitPay'
        })
        Status = 'WaitPay';
      } else if (orderState == '2') {
        that.setData({
          active1: false,
          active2: false,
          active3: true,
          active4: false,
          active5: false,
          active6: false,
          PageIndex: 1,
          OrderStatus: 'WaitDeliver'
        })
        Status = 'WaitDeliver';
      } else if (orderState == '3') {
        that.setData({
          active1: false,
          active2: false,
          active3: false,
          active4: true,
          active5: false,
          active6: false,
          PageIndex: 1,
          OrderStatus: 'WaitReceipt'
        })
        Status = 'WaitReceipt';
      } else if (orderState == '4') {
        that.setData({
          active1: false,
          active2: false,
          active3: false,
          active4: false,
          active5: true,
          active6: false,
          PageIndex: 1,
          OrderStatus: 'WaiteValuate'
        })
        Status = 'WaiteValuate';
      } else if (orderState == '5') {
        that.setData({
          active1: false,
          active2: false,
          active3: false,
          active4: false,
          active5: false,
          active6: true,
          PageIndex: 1,
          OrderStatus: 'Refund'
        })
        Status = 'Refund';
      }
      LoadOrderList(that, Status, 1);
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    //wx.removeStorageSync('OrderStatusUserInfo');
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.setData({
      PageIndex: 1
    });
    LoadOrderList(this, this.data.OrderStatus, this.data.PageIndex);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    if (this.data.IsLastPage && this.data.PageIndex != 1) {

      return false;
    }
    var index = this.data.PageIndex;
    index = index + 1;
    this.setData({
      PageIndex: index,
    })
    var that = this;
    LoadOrderList(that, that.data.OrderStatus, index);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 申请退款
   */
  onApplyRefund: function (event) {

    wx.navigateTo({
      url: '/pages/returngoods/returngoods?OrderID=' + event.currentTarget.id + "&shopID=" + event.currentTarget.dataset.shopid
    })
  },

  /**
   * 取消申请退款
   */
  onCancelRefund: function (event) {
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var url = globalData.apiurl + '/order/api/OrderInfo/CancelRefundOrder';
    var returnNo = event.target.id;
    var that = this;
    wx.showModal({
      title: '取消申请退款',
      content: '确定取消申请退款吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: url,
            data: {
              "custId": custId,
              "returnNo": returnNo,
              "unionStoreID": event.currentTarget.dataset.shopid
            },
            method: 'GET',
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              wx.showToast({
                title: '取消申请退款成功',
                duration: 2000
              })
              LoadOrderList(that, that.data.OrderStatus, 1);
            },
            fail: function () {
              wx.showToast({
                title: '取消申请退款失败',
                duration: 2000
              })
            },
            complete: function () {
              //complete
            }
          })
        }
      }
    })
  },

  /**
   * 取消订单
   */
  onCancelOrder: function (event) {
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var res = wx.getStorageSync('UserInfo');
    var userId = res.UserID;
    var url = globalData.apiurl + '/order/api/OrderInfo/CancelOrder';
    var OID = event.target.id;
    var unionStoreID = event.currentTarget.dataset.shopid;
    var that = this;
    wx.showActionSheet({
      itemList: ['我不想买了', '信息填写错误，重新拍', '其他原因'],
      success(res) {
        var reason = "其他原因";
        if (res.tapIndex == 0) {
          reason = "我不想买了";
        } else if (res.tapIndex == 1) {
          reason = "我不想买了";
        }
        wx.request({
          url: url,
          data: {
            "custId": custId,
            "userId": userId,
            "orderId": OID,
            "reason": reason,
            "unionStoreID": unionStoreID
          },
          method: 'GET',
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            wx.showToast({
              title: '取消订单成功',
              duration: 2000
            })
            LoadOrderList(that, that.data.OrderStatus, 1);
          },
          fail: function () {
            wx.showToast({
              title: '取消订单失败',
              duration: 2000
            })
          },
          complete: function () {
            //complete
          }
        })

      },
      fail(res) {

      }
    })


  },

  /**
   * 删除订单
   */
  onDeleteOrder: function (event) {
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var url = globalData.apiurl + '/order/api/OrderInfo/DeleteOrder';
    var OID = event.target.id;
    var unionStoreID = event.currentTarget.dataset.shopid;
    var that = this;
    wx.showModal({
      title: '删除订单',
      content: '确定删除该订单吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: url,
            data: {
              "custId": custId,
              "orderId": OID,
              "unionStoreID": unionStoreID
            },
            method: 'GET',
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              wx.showToast({
                title: '删除订单成功',
                duration: 2000
              })
              LoadOrderList(that, that.data.OrderStatus, 1);
            },
            fail: function () {
              wx.showToast({
                title: '删除订单失败',
                duration: 2000
              })
            },
            complete: function () {
              //complete
            }
          })
        }
      }
    })
  },

  /**
   * 确认收货
   */
  onReceiveOrder: function (event) {
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var url = globalData.apiurl + '/order/api/OrderInfo/ReceiveOrder';
    var OID = event.target.id;
    var that = this;
    var unionStoreID = event.currentTarget.dataset.shopid;
    wx.showModal({
      title: '确认收货',
      content: '确定确认收货吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: url,
            data: {
              "custId": custId,
              "orderId": OID,
              "unionStoreID": unionStoreID
            },
            method: 'GET',
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              wx.showToast({
                title: '确认收货成功',
                duration: 2000
              })
              LoadOrderList(that, that.data.OrderStatus, 1);
            },
            fail: function () {
              wx.showToast({
                title: '确认收货失败',
                duration: 2000
              })
            },
            complete: function () {
              //complete
            }
          })
        }
      }
    })
  },

  /**
   * 跳转订单详情
   */
  onRedirectToOrderInfo: function (event) {

    wx.navigateTo({
      url: '/pages/orderDetails/orderDetails?OrderID=' + event.currentTarget.id + "&shopID=" + event.currentTarget.dataset.shopid
    })
  },

  /**
   * 付款
   */
  onGoPay: function (event) {

    wx.navigateTo({
      url: '/pages/unpaid/unpaid?OrderID=' + event.currentTarget.id + "&unionShopID=" + event.currentTarget.dataset.shopid,
      complete: function (res) {
        //设置返回的时候是代付款
        //添加缓存，用缓存来接收参数
        wx.setStorageSync("OrderStatusUserInfo", 1)
      }
    })
  },
  ShowLogistics: function (event) {
    var that = this;
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var url = globalData.apiurl + '/order/api/OrderInfo/SearchOrderTraces';
    var OID = event.target.id;
    var that = this;
    var unionStoreID = event.currentTarget.dataset.shopid;
    wx.request({
      url: url,
      data: {
        "custId": custId,
        "orderId": OID,
        "unionStoreId": unionStoreID
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.code == 1200) {
          if (res.data.content.ShipperCode == "SF" || res.data.content.ShipperCode == "HTKY" || res.data.content.ShipperCode == "STO" || res.data.content.ShipperCode == "HHTT") {
            res.data.content.Reason = "快递鸟暂不支持顺丰 百世快递 申通快递 天天快递物流查询";
          }
        }
        that.setData({
          wuliuList: res.data.content,
          wulimodel: false

        });
      },
      fail: function () {

      },
      complete: function () {

      }
    })
  },
  /**
   * 立即评价
   */
  onComment: function (event) {
    wx.navigateTo({
      url: '/pages/addComment/addComment?OrderID=' + event.currentTarget.id + "&shopID=" + event.currentTarget.dataset.shopid
    })
  }

})

//获取订单列表
function LoadOrderList(obj, OrderStatus, PageIndex) {


  var globalData = getApp().globalData;
  var custId = globalData.custID;
  var res = wx.getStorageSync('UserInfo');
  var userId = res.UserID;
  var pageSize = obj.data.PageSize;
  var url = globalData.apiurl + '/order/api/OrderInfo/GetMyOrderList';
  wx.showLoading({
    title: '加载中...',
    mask: true
  });
  wx.request({
    url: url,
    data: {
      "custId": custId,
      "userId": userId,
      "orderStatus": OrderStatus,
      "pageIndex": PageIndex,
      "pageSize": pageSize,
      "OrderType": 0
    },
    method: 'GET',
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {



      wx.hideLoading();
      var list = res.data.content;
      for (var k = 0; k < list.length; k++) {
        getHtml(list[k].OrderDetails);
      }

      if (PageIndex > 1) {
        var olist = obj.data.OrderList;
        for (var i = 0, len = list.length; i < len; i++) {
          olist.push(list[i]);
        }
        obj.setData({
          OrderList: olist,
          IsLastPage: false
        });
        if (list.length < pageSize) {
          obj.setData({
            OrderList: olist,
            IsLastPage: true
          })
        }
      } else {
        if (list == null || list.length == 0) {
          obj.setData({

            OrderList: [],
            nodata: false
          });
        } else {
          obj.setData({
            nodata: true,
            OrderList: list,
            IsLastPage: false,
            nodata: true

          });
        }
      }


    },
    fail: function () {
      wx.hideLoading();
      wx.showToast({
        title: '获取列表失败',
        duration: 2000
      })
    },
    complete: function () {
      //complete
    }
  })
}

function getHtml(d) {
  if (d.length > 0) {
    for (var j = 0; j < d.length; j++) {
      if (d[j].acitivtys != null && d[j].acitivtys != "") {
        var ac = d[j].acitivtys.split(",");
        if (ac.length > 0) {
          d[j].acitivtys = "";
          for (var k = 0; k < ac.length; k++) {
            switch (ac[k]) {
              case "1":
                d[j].acitivtys += '<span class="yute zu">组</span>';
                break;
              case "2":
                d[j].acitivtys += '<span class="yute man">兑</span>';
                break;
              case "3":
                d[j].acitivtys += '<span class="yute xian">限</span>';
                break;
              case "4":
                d[j].acitivtys += '<span class="yute man">满</span>';
                break;
              case "5":
                d[j].acitivtys += '<span class="yute man">复</span>';
                break;
              case "6":
                d[j].acitivtys += '<span class="yute miao">返</span>';
                break;
              case "7":
                d[j].acitivtys += '<span class="yute tuan">拼</span>';
                break;
              case "8":
                d[j].acitivtys += '<span class="yute miao">秒</span>';
                break;
            }
          }
        }
      } else {
        d[j].acitivtys = "";
      }
    }
  }
}